package com.cndw.kungfu.model.goods.domain;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.ext.XmlProcesser;
import com.cndw.kungfu.model.Context;

public class Discount {
	
	private int id;
	private int goodsId;
	private String name;
	private int gold;
	private int discount;
	private int max;
	private int days;
	

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getGold() {
		return gold;
	}

	public void setGold(int gold) {
		this.gold = gold;
	}

	public int getDiscount() {
		return discount;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}
	

	public int getDays() {
		return days;
	}

	public void setDays(int days) {
		this.days = days;
	}


	private static final Map<Integer, Discount> dataMap = new HashMap<Integer, Discount>();

	private static final String configFile = "discount.xml";

	static {
		load();
	}

	public static synchronized void load() {
		if (dataMap.isEmpty()) {
			String fileName = ConstantsConfig.getLocaleResource() + configFile;
			List<Discount> dataList = XmlProcesser.processXml(Discount.class, fileName);
			if (dataList != null && dataList.size() > 0) {
				for (Discount e : dataList) {
					GoodsMode goodsMode = Context.getGoodsServer().getModeById(e.getGoodsId());
					if (goodsMode == null) {
						continue;
					}
					dataMap.put(e.getId(), e);
				}
			}
		}
	}

	public static synchronized void clear() {
		dataMap.clear();
		load();
	}

	public static Collection<Discount> getAll() {
		return dataMap.values();
	}

	public static Discount getOne(int id) {
		return dataMap.get(id);
	}

}
